// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "user_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "发布者ID",
    "label": "发布者ID"
  },
  "name": {
    "rules": [
      {
        "required": true,
        "errorMessage": "名称需为2-50个字符"
      },
      {
        "format": "string",
        "errorMessage": "名称需为2-50个字符"
      },
      {
        "minLength": 2,
        "maxLength": 50,
        "errorMessage": "名称需为2-50个字符"
      }
    ],
    "title": "商品名称",
    "label": "商品名称"
  },
  "price": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "minimum": 0,
        "maximum": 10000000
      }
    ],
    "title": "价格(分)",
    "label": "价格(分)"
  },
  "category": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "沙发",
            "text": "沙发"
          },
          {
            "value": "餐桌椅",
            "text": "餐桌椅"
          },
          {
            "value": "床具",
            "text": "床具"
          },
          {
            "value": "柜架",
            "text": "柜架"
          },
          {
            "value": "茶几",
            "text": "茶几"
          },
          {
            "value": "办公家具",
            "text": "办公家具"
          }
        ]
      }
    ],
    "title": "家具类别",
    "label": "家具类别"
  },
  "cover": {
    "rules": [
      {
        "format": "string"
      },
      {
        "format": "url"
      }
    ],
    "title": "封面图",
    "label": "封面图"
  },
  "gallery": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "产品图集",
    "label": "产品图集"
  },
  "status": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "上架状态",
    "defaultValue": true,
    "label": "上架状态"
  },
  "material": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "紫檀",
            "text": "紫檀"
          },
          {
            "value": "黄花梨",
            "text": "黄花梨"
          },
          {
            "value": "红酸枝",
            "text": "红酸枝"
          },
          {
            "value": "鸡翅木",
            "text": "鸡翅木"
          },
          {
            "value": "乌木",
            "text": "乌木"
          }
        ]
      }
    ],
    "title": "木材材质",
    "label": "木材材质"
  },
  "description": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "详情描述",
    "label": "详情描述"
  }
}

const enumConverter = {
  "category_valuetotext": {
    "沙发": "沙发",
    "餐桌椅": "餐桌椅",
    "床具": "床具",
    "柜架": "柜架",
    "茶几": "茶几",
    "办公家具": "办公家具"
  },
  "material_valuetotext": {
    "紫檀": "紫檀",
    "黄花梨": "黄花梨",
    "红酸枝": "红酸枝",
    "鸡翅木": "鸡翅木",
    "乌木": "乌木"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
